package com.back.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.back.entity.User;
import com.back.mapper.UserDao;
import com.back.service.inte.UserService;
import com.back.uitls.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;



    @Override
    public void register(User user) throws MyException {
        try {
            user.setPwd(SaSecureUtil.sha256(user.getPwd()));
            userDao.add(user);
        } catch (Exception e) {
            throw new MyException("用户添加失败");
        }
    }

    @Override
    public void login(String name, String password) throws MyException {
        User user = userDao.login(name, SaSecureUtil.sha256(password));
        if (user == null)
            throw new MyException("登录失败");
        StpUtil.login(user.getId());
        System.out.println(StpUtil.getLoginId());
    }

}
